package logic

import (
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/oss"
	"bytes"
	"context"
	"path"

	"MyBlogv2/Servers/Article/article"
	"MyBlogv2/Servers/Article/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UploadArticleCoverLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUploadArticleCoverLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadArticleCoverLogic {
	return &UploadArticleCoverLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UploadArticleCoverLogic) UploadArticleCover(in *Article.UploadArticleCoverRequest) (*Article.UploadArticleCoverResponse, error) {
	filePath := in.ArticleCover
	filename := in.Filename
	file := bytes.NewReader(filePath)
	ext := path.Ext(filename)
	objectName := helper.GetUUID() + ext

	uploadFilePath, err := oss.UploadFile(objectName, file)
	if err != nil {
		return &Article.UploadArticleCoverResponse{
			Code: int64(HttpCode.FILE_UPLOAD_ERROR),
		}, nil
	}
	return &Article.UploadArticleCoverResponse{
		Code: int64(HttpCode.SUCCESS),
		Data: uploadFilePath,
	}, nil
}
